<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="../../static/layui/css/layui.css" rel="stylesheet">
    <script src="../../static/layui/layui.js"></script>
    <style>
        .container{
            padding: 25px;
        }
    </style>
</head>
<body>
<!--行工具栏模板-->
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">删除</a>
        <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="reset">重置密码</a>
    </div>
</script>
<!--头部工具栏模板-->
<script type="text/html" id="userBar">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-circle-fine"></i>新增
        </a>
        <a class="layui-btn layui-btn-sm layui-bg-red" lay-event="batchDelete">
            <i class="layui-icon layui-icon-delete"></i>批量删除
        </a>
    </div>
</script>

<div class="container">
    <div class="search">
        <form class="layui-form userForm layui-row layui-col-space16">
            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" placeholder="请输入用户名" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-col-md4">
                <button class="layui-btn" lay-submit lay-filter="table-search">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
    </div>

    <table class="layui-hide" id="ID-table-demo-data" lay-filter="admin-filter"></table>
</div>
<script>
    layui.use(function(){
        // 获取表格对象
        let table = layui.table,
            form = layui.form,
            $ = layui.$,
            layer = layui.layer;
        // 已知数据渲染
        let inst = table.render({
            elem: '#ID-table-demo-data',
            id:'admin',
            url:"/house/admin/page",
            toolbar: '#userBar',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'username', title: '用户名'},
                {field: 'nickname', title: '姓名'},
                {field: 'phone', title: '联系方式'},
                {field: 'createTime', title: '创建时间',minWidth: 200},
                {fixed: 'right', title:'操作', width: 200, minWidth: 180, toolbar: '#barDemo'}
            ]],
            //分页配置
            skin: 'line',
            even: true,
            page: true,
            limits: [ 5, 10, 15],
            limit: 5
        });

        table.on('toolbar(admin-filter)', function(obj) {
            let id = obj.config.id;
            let checkStatus = table.checkStatus(id);
            let infos = checkStatus.data

            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        area: ['30%','70%'],
                        shade: 0.6,
                        shadeClose: true,
                        anim: 0,
                        end: function(){
                            // 重载表格中的数据 是在你点开新增填完后，显示成功，然后弹窗关闭，那么重载表格显示新增的数据
                            table.reload("admin",{
                                page:{
                                    curr:1,
                                    limit:5
                                }
                            })
                        },
                        content: 'add.html'
                    });
                    //  发送异步请求 到后台完成用户信息添加
                    break;
                case 'batchDelete':
                    //  箭头函数   lambda   ->
                    //遍历你选中的数据从中拿出id
                    let ids = infos.map(info=>info.id);     // []
                    // 判定用户是否选择数据
                    if (ids.length==0){
                        layer.msg("请至少选择一条数据")
                        return;
                    }
                    //  发送异步请求 到后台完成用户信息删除
                    $.ajax({
                        url:'/house/admin/delete',
                        data: JSON.stringify(ids),
                        contentType:'application/json;charset=utf-8',
                        type:"post",
                        dataType:'json',
                        success:function (res){
                            if (res.code==0){  //  删除成功
                                layer.msg(res.message,{icon:1},function (){
                                    table.reload("admin",{
                                        page:{
                                            curr:1,
                                            limit:5
                                        }
                                    } ) //  表格数据的重载
                                })
                            }else{
                                layer.msg(res.message,{icon:2})
                            }
                        }
                    })
                    break;
            }
            ;
        });

        // 触发单元格工具事件
        table.on('tool(admin-filter)', function(obj){ // 双击 toolDouble
            let data = obj.data; // 获得当前行数据
            if(obj.event === 'edit'){
                // 根据用户id  查询用户信息
                $.get('/house/admin/getById',{id:data.id},function (res){
                    if (res.code==0){
                        // 获取用户信息
                        let usr = res.data
                        sessionStorage.setItem("usr",JSON.stringify(usr))
                        // 用户信息的编辑
                        layer.open({
                            title: '编辑',
                            type: 2,
                            area: ['30%','70%'],
                            end:function (){
                                table.reload("admin")
                            },
                            content: 'edit.html'
                        });
                    }else{
                        layer.msg(res.msg,function (){
                            table.reload("admin")
                        })
                    }
                })
            } else if(obj.event === 'delete'){
                let  id = [];
                id.push(data.id)
                //  发送异步请求 根据id 删除用户信息   data.id
                $.ajax({
                    url:'/house/admin/delete',
                    data: JSON.stringify(id) ,
                    contentType:'application/json;charset=utf-8',
                    type:"post",
                    dataType:'json',
                    success:function (res){
                        if (res.code==0){  //  删除成功
                            layer.msg(res.message,{icon:1},function (){
                                table.reload("admin",{
                                    page:{
                                        curr:1,
                                        limit:5
                                    }
                                })  //  表格数据的重载
                            })
                        }else{
                            layer.msg(res.message,{icon:2})
                        }
                    }
                })
            }else if(obj.event === 'reset'){
                // 获取当前行数据id
                let id = data.id;
                $.post("/house/admin/resetPassword",{id},function (res){
                    if (res.code==0){
                        layer.msg(res.message,{icon:1})
                    }else{
                        layer.msg(res.message,{icon:2})
                    }
                })
            }
        });
        // 监听表单事件
        form.on('submit(table-search)',function (obj){
            let  searchData = obj.field;
            //  重载表格数据
            table.reload('admin', {
                where: { // 传递数据异步请求时携带的字段
                    username:searchData.username
                },
                //为了防止查询时只能在本页查询
                page:{
                    curr:1,
                    limit:5
                }
            });
            return false;
        })
    });
</script>

</body>
</html>
